package cc.rengu.igas.opms.core.service.trans.apppay;

import cc.rengu.igas.opms.common.constant.OpmsParamConstant;
import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.request.QueryBankQrCodeRequest;
import cc.rengu.igas.opms.facade.response.QueryBankQrCodeResponse;
import cc.rengu.igas.share.core.model.QrCodeInfo;
import cc.rengu.igas.share.core.realize.BompService;
import cc.rengu.igas.share.core.realize.impl.BompServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.enums.CheckResultEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.OrderInfo;
import cc.rengu.oltp.service.realize.TransCustomImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.net.URLDecoder;
import java.util.List;

/**
 * 手机银行二维码查询交易
 *
 * @author fuyp
 * @version 1.0.0 创建时间：2020.03.18
 */
public class QueryBankQrCodeService extends ManageService implements TransCustomImpl {

    @Override
    protected Object bizBeforeCust() throws Exception {
        //加载手机银行二维码查询请求类
        QueryBankQrCodeRequest queryBankQrCodeRequest = new QueryBankQrCodeRequest();
        ConvertUtil.convertOutput(queryBankQrCodeRequest);
        return queryBankQrCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        //业务处理
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        QueryBankQrCodeRequest queryBankQrCodeRequest = (QueryBankQrCodeRequest) request;
        QueryBankQrCodeResponse queryBankQrCodeResponse = new QueryBankQrCodeResponse();
        //引入响应类
        BizResponse bizResponse = new BizResponse();
        String qrUrl = null;

        if (StringUtil.isEmptyOrNull(queryBankQrCodeRequest.getQrUrl())) {
            rglog.error("二维码URL不可以为空");
            throw new BizException(RespCodeEnum.QRURL_NOT_BLANK.getRespCode(), RespCodeEnum.QRURL_NOT_BLANK.getRespDesc());
        } else {
            qrUrl = URLDecoder.decode(queryBankQrCodeRequest.getQrUrl(), "UTF-8");
        }
        //调用查询方法
        BompService bompService = new BompServiceImpl();
        QrCodeInfo qrCodeInfo = bompService.getQrCodeInfo(queryBankQrCodeRequest.getHeader().getInstId(), qrUrl, null);
        if (null == qrCodeInfo) {
            rglog.error("获取二维码信息失败");
            throw new BizException(RespCodeEnum.GET_QRCODE_INVALID.getRespCode(), RespCodeEnum.GET_QRCODE_INVALID.getRespDesc());
        } else {
            if (StringUtil.isEmptyOrNull(qrCodeInfo.getQrStatus()) || !OpmsParamConstant.QR_STATUS_NORMAL.equals(qrCodeInfo.getQrStatus())) {
                rglog.error("二维码无效");
                throw new BizException(RespCodeEnum.GET_QRCODE_INVALID.getRespCode(), RespCodeEnum.GET_QRCODE_INVALID.getRespDesc());
            }
        }

        /*判断二维码类型 */
        if ("OBQR".equals(qrCodeInfo.getQrCodeType())) {
            /*他行二维码*/
            queryBankQrCodeResponse.setQrCode(qrCodeInfo.getRelationQrCode());
        } else {
            queryBankQrCodeResponse.setQrCode(qrCodeInfo.getQrUrl());
        }
        /*为响应域赋值*/
        queryBankQrCodeResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryBankQrCodeResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(queryBankQrCodeResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        //引入业务处理响应类
        BizResponse bizResponse = (BizResponse) response;
        QueryBankQrCodeResponse queryBankQrCodeResponse = (QueryBankQrCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(queryBankQrCodeResponse);
    }

    @Override
    public ChannelCallCfg channelCallSceneDiscern(Object request, List<ChannelCallCfg> channelCallCfgList) throws Exception {
        return null;
    }

    @Override
    public CheckResultEnum channelCallPreCheck(Object request, ChannelCallCfg channelCallCfg, OrderInfo orderInfo) throws Exception {
        return null;
    }

    @Override
    public TransStatusEnum channelCallResultProc(Object request, ChannelCallCfg channelCallCfg, TransStatusEnum transStatusEnum, Object channelResponse, OrderInfo needUpdateOrderInfo) throws Exception {
        return null;
    }
}
